/**
 * Copyright (C), 2024, xxxx有限公司
 * FileName: BeanBindUtils
 * Author:   TLVM
 * Date:     3/22/2024 2:42 PM
 * Description: 加载流程
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.iqcctt.simpleflow.utils;

import com.iqcctt.simpleflow.annotation.SimpleBeanFlow;
import com.iqcctt.simpleflow.annotation.SimpleFiledFlow;
import com.iqcctt.simpleflow.exception.SimpleFlowException;
import com.iqcctt.simpleflow.typeengine.TypeFactory;
import com.iqcctt.simpleflow.typeengine.example.ToolsExtends;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 〈一句话功能简述〉<br>
 * 〈加载流程〉
 *
 * @author TLVM
 * @create 3/22/2024
 * @since 1.0.0
 */
public class BeanBindUtils extends ToolsExtends {

    public static <IQCCTT> void bind(IQCCTT object) throws Exception {
        bind(Stream.of(object).collect(Collectors.toList()));
    }

    public static <IQCCTT> void bind(List<IQCCTT> listObject) throws Exception {
        for (Object result : listObject) {
            Class<?> cls = result.getClass();
            Annotation[] annotationClss = cls.getAnnotations();
            String controlFieldName = null;
            for (Annotation annotation : annotationClss) {
                if (annotation.annotationType().isAssignableFrom(SimpleBeanFlow.class)) {
                    SimpleBeanFlow simpleBeanFlow = (SimpleBeanFlow) annotation;
                    controlFieldName = simpleBeanFlow.controlFieldName();
                }
            }
            Optional.ofNullable(controlFieldName)
                    .orElseThrow(() -> new SimpleFlowException("实体未指定字段不能为空！"));
            Field[] fields = FieldUtils.getAllFields(cls); //获取所有字段
            Object controlFieldValue = getValue(result, controlFieldName);
            for (Field field : fields) {
                field.setAccessible(true);
                loadAnnotation(field,result,controlFieldValue);

            }
        }
    }

    private static void loadAnnotation(Field field, Object result, Object controlFieldValue) throws Exception {
        Annotation[] annotationArr = field.getAnnotations();
        for (Annotation annotation : annotationArr) {
            if (annotation.annotationType().isAssignableFrom(SimpleFiledFlow.class)) {
                SimpleFiledFlow simpleFiledFlow = (SimpleFiledFlow) annotation;
                if(StringUtils.isNotBlank(simpleFiledFlow.fieldName())){
                    //使用局部控制字段进行 判断必须分开避免继续使用全局控制字段进行控制
                    if(Integer.valueOf(simpleFiledFlow.controlFieldValue())
                            ==Integer.valueOf((Integer) getValue(result, simpleFiledFlow.fieldName()))){
                        Class<?> fieldType = getField(result, field.getName()).getType();
                        TypeFactory.getInvokeStrategy(fieldType.getName()).load(result,field,simpleFiledFlow,1);
                    }
                }else if(Integer.valueOf(simpleFiledFlow.controlFieldValue())==Integer.valueOf((Integer) controlFieldValue)){
                    //使用全局控制字段进行
                    Class<?> fieldType = getField(result, field.getName()).getType();
                    TypeFactory.getInvokeStrategy(fieldType.getName()).load(result,field,simpleFiledFlow,0);
                }
            }
        }
    }

}
